package nuc.edu.tlias.exception;

import lombok.extern.slf4j.Slf4j;
import nuc.edu.tlias.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHander {


    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException e) {
        log.error("程序出错了", e);
        String message = e.getMessage();
        int i = message.indexOf("Duplicate entry");
        String errMsg = message.substring(i);
        String[] arr = errMsg.split(" ");
        return Result.error("唯一字段" + arr[2] + "重复啦！！！！！！");
    }

    @ExceptionHandler
    public Result handleException(Exception e){
        log.error("捕获到的异常类型: {}", e.getClass().getName());
        log.error("程序出错了",e);
        return Result.error("出错啦！！！！！！");
    }

}
